package com.mall.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.exception.MyException;
import com.mall.model.CommDetailed;
import com.mall.model.Commodity;
import com.mall.service.CommDetailedService;
import com.mall.service.CommodityService;
@Controller
@RequestMapping("/CommDetailedController")
public class CommDetailedController {
	@Autowired
	private CommDetailedService commDetailedService;
	@Autowired
	private CommodityService CommodityService;
	/**
	 * 添加商品详细图文
	 *
	 *@dagewang 2017年8月15日
	 */
	@RequestMapping("/addDetailed")
	public @ResponseBody  Map<String,Object> addDetailed(CommDetailed commDetailed,Integer id,Integer commid)
	throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		int result=0;
		if(id!=null){
			result=commDetailedService.update(commDetailed);
		}else{
			if(commid==null||commDetailed.getContent()==null){
				map.put("result", result);
				return map;
			}
			result =commDetailedService.save(commDetailed,commid);
		}
		map.put("result", result);
		return map;
	}
	/**
	 *获取商品详细信息,为空也可;
	 *@dagewang 2017年9月7日
	 */
	@RequestMapping("/getDetailed")
	public String getDetailed(Integer commid,Model model) throws Exception{
		if(commid==null){
			throw new MyException("没有接收到商品信息");
		}
		Commodity commById = CommodityService.getCommById(commid);
		if(commById==null){
			throw new MyException("该商品不存在");
		}
		CommDetailed commDetailed = commDetailedService.get(commid);
		if(commDetailed!=null){
			model.addAttribute("detailed", commDetailed);
		}
		model.addAttribute("comm", commById);
		return "addCommDetailed";
		
	}
	public CommDetailedService getCommDetailedService() {
		return commDetailedService;
	}
	public void setCommDetailedService(CommDetailedService commDetailedService) {
		this.commDetailedService = commDetailedService;
	}
	public CommodityService getCommodityService() {
		return CommodityService;
	}
	public void setCommodityService(CommodityService commodityService) {
		CommodityService = commodityService;
	}
	
}
